package com.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.shop.bean.OrderItem;
import com.shop.dao.IOrderItemDao;
import com.shop.util.ConnDB;

/** 
 * @className: OrderItemDaoImpl.java
 * @classDescription: 
 * @function: 
 * @author: Wentasy
 * @createTime: 2012-11-1 下午07:17:16
 * @modifyTime: 
 * @modifyReason: 
 * @since: JDK 1.6
 */
public class OrderItemDaoImpl implements IOrderItemDao {
	
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	private String sql = "";
	private int affectedLine = 0;
	private OrderItem orderItem = null;
	public int createOrderItem(OrderItem orderItem) throws Exception {
		Long orderId = orderItem.getOrderId();
		Long goodsId = orderItem.getGoodsId();
		int number = orderItem.getNumber();
		conn = ConnDB.getConn();
		sql = "insert into orderitem(orderId,goodsId,number) values(?,?,?)";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, orderId);
		pstmt.setLong(2, goodsId);
		pstmt.setInt(3, number);
		affectedLine = pstmt.executeUpdate();
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
	}

	public int deleteOrderItemById(long orderItemId) throws Exception {
		conn = ConnDB.getConn();
		sql = "delete from orderitem where orderItemId = ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, orderItemId);
		affectedLine = pstmt.executeUpdate();
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		
		return affectedLine;
	}

	public List<OrderItem> queryAllOrderItem() throws Exception {
		conn = ConnDB.getConn();
		sql = "select * from orderitem";
		List<OrderItem> orderItemList = new ArrayList<OrderItem>();
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		while(rs.next()){
			orderItem = new OrderItem();
			orderItem.setOrderItemId(rs.getLong("orderItemId"));
			orderItem.setOrderId(rs.getLong("orderId"));
			orderItem.setGoodsId(rs.getLong("goodsId"));
			orderItem.setNumber(rs.getInt("number"));
			orderItemList.add(orderItem);
		}
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return orderItemList;
	}

	
	public List<OrderItem> queryAllOrderItem(int startPage, int pageSize)
			throws Exception {
		sql = "select * from orderItem limit(?-1)*?,?";
		conn = ConnDB.getConn();
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, startPage);
		pstmt.setInt(2, pageSize);
		pstmt.setInt(3,pageSize);
		List<OrderItem> orderItemList = new ArrayList<OrderItem>();
		rs = pstmt.executeQuery();
		while(rs.next()){
			orderItem = new OrderItem();
			orderItem.setOrderItemId(rs.getLong(1));
			orderItem.setOrderId(rs.getLong(2));
			orderItem.setGoodsId(rs.getLong(3));
			orderItem.setNumber(rs.getInt(4));
			orderItemList.add(orderItem);
		}
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return orderItemList;
		
	}

	
	public OrderItem queryOrderItemById(long orderItemId) throws Exception {
		conn = ConnDB.getConn();
		sql ="select * orderItem where orderItemId =? ";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, orderItemId);
		rs = pstmt.executeQuery();
		if(rs.next()){
			orderItem = new OrderItem();
			orderItem.setOrderItemId(rs.getLong("orderItemId"));
			orderItem.setOrderId(rs.getLong("orderId"));
			orderItem.setGoodsId(rs.getLong("goodsId"));
			orderItem.setNumber(rs.getInt("number"));
		}
		return orderItem;
	}

	public List<OrderItem> queryOrderItemByOrderId(long orderId) throws Exception {
		conn = ConnDB.getConn();
		List<OrderItem> orderItemList = new ArrayList<OrderItem>();
		sql = "select * from orderItem where orderId =?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, orderId);
		rs = pstmt.executeQuery();
		while(rs.next()){
			orderItem = new OrderItem();
			orderItem.setOrderItemId(rs.getLong("orderItemId"));
			orderItem.setOrderId(rs.getLong("orderId"));
			orderItem.setGoodsId(rs.getLong("goodsId"));
			orderItem.setNumber(rs.getInt("number"));
			orderItemList.add(orderItem);
		}
		return orderItemList;
	}

	public int updateOrderItem(OrderItem orderItem) throws Exception {
		Long orderItemId = orderItem.getOrderItemId();
		Long orderId = orderItem.getOrderId();
		Long goodsId = orderItem.getGoodsId();
		int number = orderItem.getNumber();
		conn = ConnDB.getConn();
		sql ="update orderItem set orderId=?,goodsId=?,number=? where orderItemId = ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, orderId);
		pstmt.setLong(2, goodsId);
		pstmt.setInt(3, number);
		pstmt.setLong(4, orderItemId);
		affectedLine = pstmt.executeUpdate();
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
		
	}

}
